﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;
using System.Data;

namespace CRM.Web.VehicleManage
{
    public partial class SaleList : Common.PageBase
    {
        public string ModifyUrl = "SaleModify.aspx?VEHICLEID={0}";
        T_SALEVEHICLE_BLL bll = new T_SALEVEHICLE_BLL();
        private T_VEHICLECUSTOMER_BLL vehicleCustomerBll = new T_VEHICLECUSTOMER_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            gvList.PageSize = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PageSize"), 20);
            PageHelper.SetPrePageUrl(this.Page);
            //PageHelper.SetPopCalender(this.Page, txtOFFLINEDATEBegin);
            //PageHelper.SetPopCalender(this.Page, txtOFFLINEDATEEnd);
            //PageHelper.SetPopCalender(this.Page, txtFIRSTSALESDATEBegin);
            //PageHelper.SetPopCalender(this.Page, txtFIRSTSALESDATEEnd);
            //PageHelper.SetPopCalender(this.Page, txtMANUFACTUREDATEBegin);
            //PageHelper.SetPopCalender(this.Page, txtMANUFACTUREDATEEnd);
            PageHelper.SetPopCalender(this.Page, txtPURCHASEDATEBegin);
            PageHelper.SetPopCalender(this.Page, txtPURCHASEDATEEnd);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEEnd);
            //PageHelper.SetPopCalender(this.Page, txtCREATETIMEBegin);
            //PageHelper.SetPopCalender(this.Page, txtCREATETIMEEnd);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEBegin);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEEnd);
            //PageHelper.SetPopCalender(this.Page, txtLASTSALEDATEBegin);
            //PageHelper.SetPopCalender(this.Page, txtLASTSALEDATEEnd);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblExportMaxRows.Text = configBll.GetCacheValueByName(this.Page, "ExportMaxRows");
                txtRecordBegin.Text = StaticConstant.RecordBegin;
                txtRecordEnd.Text = StaticConstant.RecordEnd;
                ddlCUSTOMERIDBinding();
                ddlMARKETDEPARTMENTIDBinding();
                ddlPRODUCTIDBinding();
                ddlCREATEPEOPLEIDBinding();
                ddlLASTMODIFYPEOPLEIDBinding();
                ddlBUSINESSDIVISIONIDBinding();

                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定


        /// <summary>
        ///	客户编号 下拉列表数据绑定
        /// </summary>
        private void ddlCUSTOMERIDBinding()
        {
           
        }

        /// <summary>
        ///	市场部编号 下拉列表数据绑定
        /// </summary>
        private void ddlMARKETDEPARTMENTIDBinding()
        {
            
        }



        /// <summary>
        ///	产品ID 下拉列表数据绑定
        /// </summary>
        private void ddlPRODUCTIDBinding()
        {
        }

        /// <summary>
        ///	创建人编号 下拉列表数据绑定
        /// </summary>
        private void ddlCREATEPEOPLEIDBinding()
        {
            
        }



        /// <summary>
        ///	最后修改人编号 下拉列表数据绑定
        /// </summary>
        private void ddlLASTMODIFYPEOPLEIDBinding()
        {
            
        }

        /// <summary>
        ///	事业部编号 下拉列表数据绑定
        /// </summary>
        private void ddlBUSINESSDIVISIONIDBinding()
        {
           
        }


        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            #region 链接条件
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLEID"]))
            {
                sql.Append(" and VEHICLEID like '%" + Request.QueryString["VEHICLEID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VINCODE"]))
            {
                sql.Append(" and VINCODE like '%" + Request.QueryString["VINCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PRODUCTCODE"]))
            {
                sql.Append(" and PRODUCTCODE like '%" + Request.QueryString["PRODUCTCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BRANDCODE"]))
            {
                sql.Append(" and BRANDCODE like '%" + Request.QueryString["BRANDCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLETYPECODE"]))
            {
                sql.Append(" and VEHICLETYPECODE like '%" + Request.QueryString["VEHICLETYPECODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SERIESCODE"]))
            {
                sql.Append(" and SERIESCODE like '%" + Request.QueryString["SERIESCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MODELSCODE"]))
            {
                sql.Append(" and MODELSCODE like '%" + Request.QueryString["MODELSCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMODELCODE"]))
            {
                sql.Append(" and ENGINEMODELCODE like '%" + Request.QueryString["ENGINEMODELCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMANUFACTURERSCODE"]))
            {
                sql.Append(" and ENGINEMANUFACTURERSCODE like '%" + Request.QueryString["ENGINEMANUFACTURERSCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BRAND"]))
            {
                sql.Append(" and BRAND like '%" + Request.QueryString["BRAND"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLETYPE"]))
            {
                sql.Append(" and VEHICLETYPE like '%" + Request.QueryString["VEHICLETYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SERIES"]))
            {
                sql.Append(" and SERIES like '%" + Request.QueryString["SERIES"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MODELS"]))
            {
                sql.Append(" and MODELS like '%" + Request.QueryString["MODELS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMODEL"]))
            {
                sql.Append(" and ENGINEMODEL like '%" + Request.QueryString["ENGINEMODEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMANUFACTURERS"]))
            {
                sql.Append(" and ENGINEMANUFACTURERS like '%" + Request.QueryString["ENGINEMANUFACTURERS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINENUMBER"]))
            {
                sql.Append(" and ENGINENUMBER like '%" + Request.QueryString["ENGINENUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FRAMENUMBER"]))
            {
                sql.Append(" and FRAMENUMBER like '%" + Request.QueryString["FRAMENUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["RANGE"]))
            {
                sql.Append(" and RANGE like '%" + Request.QueryString["RANGE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OFFLINEDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("OFFLINEDATE", Request.QueryString["OFFLINEDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OFFLINEDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("OFFLINEDATE", Request.QueryString["OFFLINEDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FIRSTSALESDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("FIRSTSALESDATE", Request.QueryString["FIRSTSALESDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FIRSTSALESDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("FIRSTSALESDATE", Request.QueryString["FIRSTSALESDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]))
            {
                sql.Append(" and CUSTOMERID = '" + Request.QueryString["CUSTOMERID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERCODE"]))
            {
                sql.Append(" and CUSTOMERCODE like '%" + Request.QueryString["CUSTOMERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERNAME"]))
            {
                sql.Append(" and CUSTOMERNAME like '%" + Request.QueryString["CUSTOMERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MANUFACTUREDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("MANUFACTUREDATE", Request.QueryString["MANUFACTUREDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MANUFACTUREDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("MANUFACTUREDATE", Request.QueryString["MANUFACTUREDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PURCHASEDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("PURCHASEDATE", Request.QueryString["PURCHASEDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PURCHASEDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("PURCHASEDATE", Request.QueryString["PURCHASEDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MARKETDEPARTMENTID"]))
            {
                sql.Append(" and MARKETDEPARTMENTID = '" + Request.QueryString["MARKETDEPARTMENTID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MARKETDEPARTMENTCODE"]))
            {
                sql.Append(" and MARKETDEPARTMENTCODE like '%" + Request.QueryString["MARKETDEPARTMENTCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MARKETDEPARTMENTNAME"]))
            {
                sql.Append(" and MARKETDEPARTMENTNAME like '%" + Request.QueryString["MARKETDEPARTMENTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERCODE"]))
            {
                sql.Append(" and DEALERCODE like '%" + Request.QueryString["DEALERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERNAME"]))
            {
                sql.Append(" and DEALERNAME like '%" + Request.QueryString["DEALERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PURCHASEMETHOD"]))
            {
                sql.Append(" and PURCHASEMETHOD like '%" + Request.QueryString["PURCHASEMETHOD"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISSUBSIDIES"]))
            {
                sql.Append(" and ISSUBSIDIES like '%" + Request.QueryString["ISSUBSIDIES"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SALESTYPE"]))
            {
                sql.Append(" and SALESTYPE like '%" + Request.QueryString["SALESTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CONTACTNAME"]))
            {
                sql.Append(" and CONTACTNAME like '%" + Request.QueryString["CONTACTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CONTACTTEL"]))
            {
                sql.Append(" and CONTACTTEL like '%" + Request.QueryString["CONTACTTEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSMOBILEPHONE"]))
            {
                sql.Append(" and OWNERSMOBILEPHONE like '%" + Request.QueryString["OWNERSMOBILEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLESTATUS"]))
            {
                sql.Append(" and VEHICLESTATUS like '%" + Request.QueryString["VEHICLESTATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PRODUCTID"]))
            {
                sql.Append(" and PRODUCTID = '" + Request.QueryString["PRODUCTID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CFGPRODUCTCODE"]))
            {
                sql.Append(" and CFGPRODUCTCODE like '%" + Request.QueryString["CFGPRODUCTCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]))
            {
                sql.Append(" and BUSINESSDIVISIONID = '" + Request.QueryString["BUSINESSDIVISIONID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONNAME"]))
            {
                sql.Append(" and BUSINESSDIVISIONNAME like '%" + Request.QueryString["BUSINESSDIVISIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONCODE"]))
            {
                sql.Append(" and BUSINESSDIVISIONCODE like '%" + Request.QueryString["BUSINESSDIVISIONCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CARNUMBER"]))
            {
                sql.Append(" and CARNUMBER like '%" + Request.QueryString["CARNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CHASSISNO"]))
            {
                sql.Append(" and CHASSISNO like '%" + Request.QueryString["CHASSISNO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SERIALNUMBER"]))
            {
                sql.Append(" and SERIALNUMBER like '%" + Request.QueryString["SERIALNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GEARTYPE"]))
            {
                sql.Append(" and GEARTYPE like '%" + Request.QueryString["GEARTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GEARCODE"]))
            {
                sql.Append(" and GEARCODE like '%" + Request.QueryString["GEARCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LICENSECODE"]))
            {
                sql.Append(" and LICENSECODE like '%" + Request.QueryString["LICENSECODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTSALEDATEBegin"]))
            {
                sql.Append(" and  " + bll._dal.GetBeginDateString("LASTSALEDATE", Request.QueryString["LASTSALEDATEBegin"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTSALEDATEEnd"]))
            {
                sql.Append(" and  " + bll._dal.GetEndDateString("LASTSALEDATE", Request.QueryString["LASTSALEDATEEnd"]));
            }
            if (!String.IsNullOrEmpty(Request.QueryString["INVOICECODE"]))
            {
                sql.Append(" and INVOICECODE like '%" + Request.QueryString["INVOICECODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SALESPRICE"]))
            {
                sql.Append(" and SALESPRICE like '%" + Request.QueryString["SALESPRICE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PRICE"]))
            {
                sql.Append(" and PRICE like '%" + Request.QueryString["PRICE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COLOR"]))
            {
                sql.Append(" and COLOR like '%" + Request.QueryString["COLOR"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BULLETINCODE"]))
            {
                sql.Append(" and BULLETINCODE like '%" + Request.QueryString["BULLETINCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MODIFYCOUNT"]))
            {
                sql.Append(" and MODIFYCOUNT like '%" + Request.QueryString["MODIFYCOUNT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["QUICKCODE"]))
            {
                sql.Append(" and QUICKCODE like '%" + Request.QueryString["QUICKCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SIMPLELICENCE"]))
            {
                sql.Append(" and SIMPLELICENCE like '%" + Request.QueryString["SIMPLELICENCE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PURPOSE"]))
            {
                sql.Append(" and PURPOSE like '%" + Request.QueryString["PURPOSE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLYCODE"]))
            {
                sql.Append(" and SUPPLYCODE like '%" + Request.QueryString["SUPPLYCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUPPLYNAME"]))
            {
                sql.Append(" and SUPPLYNAME like '%" + Request.QueryString["SUPPLYNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SERIALMENO"]))
            {
                sql.Append(" and SERIALMENO like '%" + Request.QueryString["SERIALMENO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATIONCODE"]))
            {
                sql.Append(" and STATIONCODE like '%" + Request.QueryString["STATIONCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMISSIONS"]))
            {
                sql.Append(" and EMISSIONS like '%" + Request.QueryString["EMISSIONS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATIONNAME"]))
            {
                sql.Append(" and STATIONNAME like '%" + Request.QueryString["STATIONNAME"] + "%'");
            }
            
            #endregion
            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
                UserType userType = UserLoginInfo.GetCurrentUserType();
                switch (userType)
                {
                    case UserType.中心部门:
                    case UserType.供应商:
                    case UserType.市场部:
                        {

                        }
                        break;
                    case UserType.服务商:
                        {
                            sql.Append(String.Format(" and STATIONCODE = '{0}' ", UserLoginInfo.GetCurrentDepartmentCode()));
                        }
                        break;
                    case UserType.经销商:
                        {
                            sql.Append(String.Format(" and DEALERCODE = '{0}' ", UserLoginInfo.GetCurrentDepartmentCode()));
                        }
                        break;
                }
            }
            
            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_SALEVEHICLE_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "VEHICLEID", "VINCODE" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string fileName = "salevehicle";
            fileName += _userLoginInfo.UserEntity.LOGINNAME.ToUpper();
           
            if (UserLoginInfo.IsAdministrator())
            {
                PageHelper.ShowMessage(this.Page, "请不要用系统管理导出数据！");
                return;
            }

            int recordBegin = StringHelper.ToInt(txtRecordBegin.Text.Trim(), 0);
            if (recordBegin != 0)
            {
                recordBegin--;
            }
            int recordEnd = StringHelper.ToInt(txtRecordEnd.Text.Trim(), 0);
            if (recordEnd != 0)
            {
                recordEnd--;
            }
            int recordCount = recordEnd - recordBegin;
            if (recordCount >= StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "ExportMaxRows"), 10000))
            {
                PageHelper.ShowMessage("导出区间大于单次导出数！");
                return;
            }

            StringBuilder sql = GenerationSQLString();
            DataTable dataTable = bll.GetDataTable(sql.ToString(), recordBegin, recordCount, gvList.SortConditions);

            

            List<NPOIExcelHelper.FieldInfo> fieldInfos = new List<NPOIExcelHelper.FieldInfo>();

            #region 导出数据列
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VEHICLEID", "车辆编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VINCODE", "整机编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PRODUCTCODE", "产品代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BRANDCODE", "品牌代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VEHICLETYPECODE", "类型代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SERIESCODE", "系列代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MODELSCODE", "机型代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ENGINEMODELCODE", "发动机型号代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ENGINEMANUFACTURERSCODE", "发动机厂家代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BRAND", "品牌"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VEHICLETYPE", "类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SERIES", "系列"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MODELS", "机型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ENGINEMODEL", "发动机型号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ENGINEMANUFACTURERS", "发动机厂家"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ENGINENUMBER", "发动机编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("FRAMENUMBER", "车架号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("RANGE", "里程"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("OFFLINEDATE", "下线日期"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("FIRSTSALESDATE", "销售日期"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERID", "客户编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERCODE", "客户代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERNAME", "客户姓名"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MANUFACTUREDATE", "出厂日期"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PURCHASEDATE", "购机日期"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MARKETDEPARTMENTID", "市场部编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MARKETDEPARTMENTCODE", "市场部代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MARKETDEPARTMENTNAME", "市场部名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEALERCODE", "经销商代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEALERNAME", "经销商名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PURCHASEMETHOD", "购机方式"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ISSUBSIDIES", "是否政府补贴"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SALESTYPE", "销售类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CONTACTNAME", "联系人姓名"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CONTACTTEL", "联系人电话"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VEHICLESTATUS", "车辆状态"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PRODUCTID", "产品ID"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CFGPRODUCTCODE", "临时产品编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATETIME", "创建时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEPEOPLEID", "创建人编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEPEOPLE", "创建人"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYPEOPLEID", "最后修改人编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYPEOPLE", "最后修改人"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYTIME", "最后修改时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("REMARK", "备注"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONID", "事业部编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONNAME", "事业部名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONCODE", "事业部代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CARNUMBER", "上车编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CHASSISNO", "底盘编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SERIALNUMBER", "出厂编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("GEARTYPE", "变速箱型号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("GEARCODE", "变速箱号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LICENSECODE", "车牌号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTSALEDATE", "二次销售日期"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("INVOICECODE", "销售发票号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SALESPRICE", "批发价"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PRICE", "零售价"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("COLOR", "车身颜色"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BULLETINCODE", "公告号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MODIFYCOUNT", "更新次数"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("QUICKCODE", "速查码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SIMPLELICENCE", "车牌简码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PURPOSE", "购车用途"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SUPPLYCODE", "供应商编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SUPPLYNAME", "供应商名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SERIALMENO", "总成系列号格式定义"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("STATIONCODE", "服务商代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("EMISSIONS", "排量"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("STATIONNAME", "服务商名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("OWNERSMOBILEPHONE", "车主手机"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("OWNERSOFFICEPHONE", "车主办公电话"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("OWNERSHOMEPHONE", "车主家庭电话"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ADDRESS", "详细地址"));


            #endregion

            NPOIExcelHelper.MemoryStreamMaxRows = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "MemoryStreamMaxRows"), 0);
            fileName = NPOIExcelHelper.DataTableToExcel(dataTable, fieldInfos, fileName + ".xls");
            UserLoginInfo.WriteLog(LogType.导出, "售后车辆导出", "售后车辆管理", fileName);
            dataTable.Clear();
            dataTable = null;
            GC.Collect();
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_SALEVEHICLE_Entity entity = new T_SALEVEHICLE_Entity();
            entity.VEHICLEID = StringHelper.ToLong((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());

            long rel = bll.Delete(entity);
            vehicleCustomerBll.RemoveVehicleCustomerInfo((sender as GridView).DataKeys[e.RowIndex].Values["VINCODE"].ToString(), _userLoginInfo);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "ISHASGPS":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = ((IsHasGPS)Enum.Parse(typeof(IsHasGPS), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {
                                                //throw;
                                            }
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
